﻿using System;
using System.Collections.ObjectModel;
using System.Windows.Threading;
using TravisLight.Model.Entity;
using TravisLight.Model.Repository;

namespace TravisLight.ViewModel
{
    public class ListViewModel
    {
        #region attributes

        private IRepository repository;

        private string username = "willdurand";

        #endregion

        #region properties
        
        public ObservableCollection<Repo> Repositories
        {
            get;
            private set;
        }

        #endregion

        /// <summary>
        /// Default constructor
        /// </summary>
        /// <param name="repository">An instance of IRepository</param>
        public ListViewModel(IRepository repository)
        {
            this.repository = repository;
            Repositories    = new ObservableCollection<Repo>(repository.GetRepositoriesFor(username));

            DispatcherTimer timer = new DispatcherTimer();
            timer.Tick += new EventHandler(RefreshRepositories);
            timer.Interval = new TimeSpan(0, 0, 5);
            timer.Start();
        }

        private void RefreshRepositories(object sender, EventArgs e)
        {
            // TODO: find a better way to do that...
            Repositories.Clear();
            foreach (Repo repo in repository.GetRepositoriesFor(username)) {
                Repositories.Add(repo);
            }
        }
    }
}
